package org.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.demo.entity.TrainingPlan;
import org.example.demo.entity.User;
import org.example.demo.service.TrainingPlanService;
import org.example.demo.util.AjaxResult;
import org.example.demo.util.ReturnMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("training/plan")
public class TrainingPlanController {

    @Resource
    private TrainingPlanService trainingPlanService;

    // 分页查询培训计划列表
    @PostMapping("selectPage")
    public AjaxResult selectPage(@RequestBody TrainingPlan plan,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();

//        ReturnMap returnMap = new ReturnMap();
        Page<TrainingPlan> page = new Page<>(pageNum, pageSize);

        QueryWrapper<TrainingPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(null != plan.getPlanName() && !plan.getPlanName().isEmpty(),
                        TrainingPlan::getPlanName, plan.getPlanName())
                .eq(plan.getPlanType() != null,
                        TrainingPlan::getPlanType, plan.getPlanType())
                .eq(plan.getStatus() != null,
                        TrainingPlan::getStatus, plan.getStatus())
                .between(plan.getStartDate() != null && plan.getEndDate() != null,
                        TrainingPlan::getStartDate, plan.getStartDate(), plan.getEndDate())
                .orderByDesc(TrainingPlan::getCreateTime);

        Page<TrainingPlan> result = trainingPlanService.page(page, queryWrapper);
        map.put("page", result);
//        returnMap.setData("page", result);
        return AjaxResult.success(map);
    }

    // 查询培训计划详情
    @GetMapping("detail")
    public AjaxResult detail(@RequestParam Integer id) {
//        ReturnMap returnMap = new ReturnMap();

        TrainingPlan plan = trainingPlanService.getById(id);

        Map<String, Object> map = new HashMap<>();
        map.put("plan", plan);
//        returnMap.setData("plan", plan);
        return AjaxResult.success(map);
    }



    //分页需要查询总条数
    @PostMapping("selectCount")
    public AjaxResult selectCount(@RequestBody TrainingPlan plan) {
//        ReturnMap returnMap = new ReturnMap();
        QueryWrapper<TrainingPlan> queryWrapper = new QueryWrapper<>();
        Integer count = trainingPlanService.count(queryWrapper.lambda()
                .like(null != plan.getPlanName() && !plan.getPlanName().isEmpty(),
                        TrainingPlan::getPlanName, plan.getPlanName())
                .eq(plan.getPlanType() != null,
                        TrainingPlan::getPlanType, plan.getPlanType())
                .eq(plan.getStatus() != null,    TrainingPlan::getStatus, plan.getStatus()));
//        returnMap.setData("count", count);

        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        return AjaxResult.success(map);
    }

    // 新增/修改培训计划
    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody TrainingPlan plan) {
//        ReturnMap returnMap = new ReturnMap();

        if (plan.getId() != null) {
            plan.setUpdateTime(LocalDateTime.now());
            trainingPlanService.updateById(plan);
        } else {
            plan.setCreateTime(LocalDateTime.now());
            plan.setUpdateTime(LocalDateTime.now());
            trainingPlanService.save(plan);
        }

//        returnMap.setReturnCode(200);
        return AjaxResult.success();
    }



    // 删除培训计划
    @DeleteMapping("delete")
    public AjaxResult delete(@RequestParam Integer id) {
//        ReturnMap returnMap = new ReturnMap();
        boolean success = trainingPlanService.removeById(id);
//        returnMap.setReturnCode(success ? 200 : 405);
//        return returnMap.getreturnMap();
        if (success) return AjaxResult.success();
        return AjaxResult.error();
    }

    // 获取当前有效的培训计划
    @GetMapping("activePlans")
    public AjaxResult getActivePlans() {
//        ReturnMap returnMap = new ReturnMap();
        QueryWrapper<TrainingPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .le(TrainingPlan::getStartDate, LocalDateTime.now())
                .ge(TrainingPlan::getEndDate, LocalDateTime.now())
                .eq(TrainingPlan::getStatus, 1) // 1-进行中
                .orderByAsc(TrainingPlan::getStartDate);

//        returnMap.setData("plans", trainingPlanService.list(queryWrapper));
        Map<String, Object> map = new HashMap<>();
        map.put("plans", trainingPlanService.list(queryWrapper));
        return AjaxResult.success(map);
    }
}
